﻿using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnalysisSystem
{
    class CreateChart
    {
        /// <summary>
        /// 创建一个chartControl并返回
        /// 需要自己添加到相应的control this.control.add(chartControl)
        /// </summary>
        /// <returns>创建好的chartControl</returns>
        public static DevExpress.XtraCharts.ChartControl CreateNewChart()
        {
            DevExpress.XtraCharts.ChartControl newChartControl = new DevExpress.XtraCharts.ChartControl();
            //默认为全填充
            newChartControl.Dock = System.Windows.Forms.DockStyle.Fill;
            newChartControl.Legend.Name = "默认";
            newChartControl.Legend.MarkerMode = LegendMarkerMode.CheckBoxAndMarker;
            newChartControl.Location = new System.Drawing.Point(200, 160);
            newChartControl.Name = "newChartControl";
            newChartControl.SeriesSerializable = new DevExpress.XtraCharts.Series[0];
            newChartControl.Size = new System.Drawing.Size(1033, 727);
            newChartControl.TabIndex = 6;
            return newChartControl;
        }

        /// <summary>
        /// 创建一个series并返回
        /// </summary>
        /// <param name="name">series的名字</param>
        /// <param name="type">series图的样式</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="argumentDataMember">横轴参数</param>
        /// <param name="valueDataMembers">纵轴参数</param>
        /// <returns>创建好的series</returns>
        public static Series CreateSeries(string name, ViewType type, in DataTable dataSource, string argumentDataMember, string valueDataMembers)
        {
            Series newSeries = new Series(name, type);
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                if (dataSource.Rows[i][valueDataMembers].Equals(DBNull.Value)) continue;
                double y = Convert.ToDouble(dataSource.Rows[i][valueDataMembers]);
                if (y.Equals(0)) continue;
                if (y.Equals(65535)) continue;
                if (argumentDataMember.Equals("日期"))
                {
                    DateTime x = Convert.ToDateTime(dataSource.Rows[i][argumentDataMember]);
                    newSeries.Points.AddPoint(x, y);
                }
                else
                {
                    double x = Convert.ToDouble(dataSource.Rows[i][argumentDataMember]);
                    if (x.Equals(65535)) continue;
                    newSeries.Points.AddPoint(x, y);
                }
            }
            //newSeries.DataSource = dataSource;
            //newSeries.ArgumentDataMember = argumentDataMember;
            //newSeries.ValueDataMembers.AddRange(valueDataMembers);
            return newSeries;
        }
    }
}
